package cn.stylefeng.guns.modular.recy.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.hutool.db.DaoTemplate;
import cn.stylefeng.guns.modular.recy.entity.CommandOrder;
import cn.stylefeng.guns.modular.recy.service.CommandOrderService;
import cn.stylefeng.guns.modular.system.service.DictService;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * 任务执行类
 */
@Component
@DependsOn({ "springContextHolder" })
public class QuartzJob implements Job {

    private CommandOrderService commandOrderService;
    public QuartzJob() {
        this.commandOrderService = (CommandOrderService) SpringContextHolder.getBean(CommandOrderService.class);
    }
    @Override
    public void execute(JobExecutionContext content) throws JobExecutionException {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "★★★★★★★★★★★");
        String jobName = content.getJobDetail().getDescription();
        JobDataMap dataMap = content.getJobDetail().getJobDataMap();
        Long binsId = dataMap.getLong("binsId");
        String commandTag = dataMap.getString("commandTag");
        QueryWrapper<CommandOrder> commandOrderQueryWrapper = new QueryWrapper<>();
        commandOrderQueryWrapper.lambda().eq(CommandOrder::getCommandTag,commandTag).eq(CommandOrder::getIsFinish,0);
        int count = this.commandOrderService.count(commandOrderQueryWrapper);
        if(count<=0) {
            CommandOrder commandOrder = CommandOrder.builder().binsId(binsId).commandTag(commandTag).createTime(new Date()).isRead(0).isFinish(0).build();
            this.commandOrderService.save(commandOrder);
            System.out.println("增加"+commandTag+"任务!");
        }else{
            //boolean remove = this.commandOrderService.remove(commandOrderQueryWrapper);
            System.err.println("存在"+commandTag+"任务未完成！");
        }
        System.out.println("传递的参数是=" + binsId+","+commandTag + "任务名字是=" + jobName);
    }
}